package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RProAllotBean;
import com.xoms.admin.controller.res.bean.RProAllotDetailBean;
import com.xoms.admin.controller.res.bean.RProBreakageBean;
import com.xoms.admin.controller.res.bean.RProBreakageInfoBean;
import com.xoms.admin.controller.res.bean.RProPureBackBean;
import com.xoms.admin.controller.res.bean.RProPureBackInfoBean;
import com.xoms.admin.controller.springmvc.req.DelAllotParam;
import com.xoms.admin.controller.springmvc.req.DelBreakageParam;
import com.xoms.admin.controller.springmvc.req.DelPureBackParam;
import com.xoms.admin.controller.springmvc.req.EditAllotParam;
import com.xoms.admin.controller.springmvc.req.EditBreakageParam;
import com.xoms.admin.controller.springmvc.req.EditPureBackParam;
import com.xoms.admin.controller.springmvc.req.GetAllotDetailsParam;
import com.xoms.admin.controller.springmvc.req.GetAllotListParam;
import com.xoms.admin.controller.springmvc.req.GetBreakageDetailsParam;
import com.xoms.admin.controller.springmvc.req.GetProBreakagesParam;
import com.xoms.admin.controller.springmvc.req.GetProPureBacksParam;
import com.xoms.admin.controller.springmvc.req.GetPureBackDetailsParam;
import com.xoms.admin.controller.springmvc.req.SubBreakageParam;
import com.xoms.admin.controller.springmvc.req.SubPureBackParam;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.ProAllotService;
import com.xoms.admin.service.ProBreakageService;
import com.xoms.admin.service.ProPureBackService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

@Component
public class ProReduceController {

	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	ProAllotService proAllotServiceImpl;

	@Autowired
	ProBreakageService proBreakServiceImpl;
	
	@Autowired
	ProPureBackService proPureBackServiceImpl;
	
	

	/**
	 * 获取商品调拨列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProAllotBean> getAllotList(GetAllotListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProAllotBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String key = param.getKey();
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();
			String endTime = param.getEndTime();
			String fromId = param.getFromId();
			String toId = param.getToId();
			// 获取调拨单列表
			PageResult<RProAllotBean> resData = proAllotServiceImpl.getAllots(account, key, page, PageSize, startTime,
					endTime, fromId, toId);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取调拨详情列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProAllotDetailBean> getAllotDetails(GetAllotDetailsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProAllotDetailBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			String allotId = param.getAllotId();
			String key = param.getKey();
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			int paging = NumUtil.toInt(param.getPaging(), 1);// 是否分页
			// 查询详情列表
			PageResult<RProAllotDetailBean> resData = proAllotServiceImpl.getAllotDetail(account, key, page, PageSize,
					allotId, paging);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 编辑/新建调拨单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProAllotBean> editAllot(EditAllotParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProAllotBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String aid = param.getAid();
			String fromId = param.getFromId();
			String remark = param.getRemark();
			String toId = param.getToId();
			int type = NumUtil.toInt(param.getType(), 1);
			String details = param.getDetails();
			String targetId = param.getTargetId();
			// 成功后返回编辑后商品调拨单信息
			RProAllotBean data = proAllotServiceImpl.edit(account, aid, fromId, toId, remark, type, details, targetId);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 删除调拨单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delAllot(DelAllotParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String aid = param.getAid();// 商品调拨单id
			boolean flag = proAllotServiceImpl.del(account, aid);// 删除调拨单信息
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取退货列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProPureBackBean> getPureBacks(GetProPureBacksParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProPureBackBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();// 起始时间
			String endTime = param.getStartTime();// 终止时间
			String storeId = param.getStoreHouseId();// 仓库id

			// 获取退货列表
			PageResult<RProPureBackBean> resData = proPureBackServiceImpl.getPureBacks(account, page, PageSize,
					startTime, endTime, storeId);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取退货商品明细列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProPureBackInfoBean> getPureBackDetails(GetPureBackDetailsParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProPureBackInfoBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			String key = param.getKey();// 关键字
			String bid = param.getBid();// 退货id
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			int paging = NumUtil.toInt(param.getPaging(), 0);// 默认不分页
			// 获取采购详情列表
			PageResult<RProPureBackInfoBean> resData = proPureBackServiceImpl.getPureBackDetails(account, key, page, PageSize,
					bid, paging);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑/新建退货单
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProPureBackBean> editPureBack(EditPureBackParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProPureBackBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String bid = param.getBid();
			String purchaseId = param.getPurchaseId(); // 关联采购单id
			String remark = param.getRemark(); // 备注
			String details = param.getDetails(); // 退货详情
			int isSubmit = NumUtil.toInt(param.getIsSubmit(), 1);// 是否直接提交1保存2直接提交

			// 成功后返回编辑后商品采购单信息
			RProPureBackBean data = proPureBackServiceImpl.edit(account, bid, purchaseId, remark, details, isSubmit);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除退货单id
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delPureBack(DelPureBackParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String bid = param.getBid();
			boolean flag = proPureBackServiceImpl.del(account, bid);// 删除退货单信息
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 提交审核退货单
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult submitPureBack(SubPureBackParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String bid = param.getBid();
			boolean flag = proPureBackServiceImpl.submitPureBack(account, bid);// 提交审核
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取报损列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProBreakageBean> getBreakages(GetProBreakagesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProBreakageBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();// 起始时间
			String endTime = param.getStartTime();// 终止时间
			String storeId = param.getStoreHouseId();// 仓库id

			// 获取退货列表
			PageResult<RProBreakageBean> resData = proBreakServiceImpl.getBreakages(account, page, PageSize,
					startTime, endTime, storeId);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 获取报损商品明细表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProBreakageInfoBean> getBreakageDetails(GetBreakageDetailsParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProBreakageInfoBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			String bid = param.getBid();// 退货id
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			int paging = NumUtil.toInt(param.getPaging(), 0);// 默认不分页
			// 获取采购详情列表
			PageResult<RProBreakageInfoBean> resData = proBreakServiceImpl.getBreakageDetails(account, page, PageSize,
					bid, paging);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑报损
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProBreakageBean> editBreakage(EditBreakageParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProBreakageBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			String bid = param.getBid();// 为空则新建
			String storeId = param.getStoreHouseId();// 仓库id
			String remark = param.getRemark();// 描述
			String details = param.getDetails();// 采购详情
			int isSubmit = NumUtil.toInt(param.getIsSubmit(), 1);// 是否直接提交1保存2直接提交

			// 成功后返回编辑后商品采购单信息
			RProBreakageBean data = proBreakServiceImpl.edit(account, bid, storeId, remark, details, isSubmit);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除报损
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delBreakage(DelBreakageParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String bid = param.getBid();// 商品报损单id
			boolean flag = proBreakServiceImpl.del(account, bid);// 删除报损单信息
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 提交报损审核
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult submitBreakage(SubBreakageParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String bid = param.getBid();// 商品报损单id
			boolean flag = proBreakServiceImpl.submitPurchase(account, bid);// 提交报损
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
